package com.lvhx.config;

import com.lvhx.utils.DataAccessManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.support.TransactionTemplate;

@Configuration
public class BeanConfig {

    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);  // 这里设置阻塞队列大小
        executor.setThreadNamePrefix("myThreadPool-");
        executor.initialize();
        return executor;
    }

    @Bean
    public TransactionTemplate nestableTransactionTemplate() {
        TransactionTemplate template = new TransactionTemplate();
        template.setTransactionManager(transactionManager);
        template.setPropagationBehaviorName("PROPAGATION_REQUIRES_NEW");
        template.setIsolationLevelName("ISOLATION_READ_COMMITTED");
        template.setReadOnly(false);
        template.setTimeout(150);
        return template;
    }
}
